" vim: fdm=marker ts=4 sw=4
let g:plug_root = get(g:, 'plug_root', g:vimfiles . '/pack/github.com/opt/')
let g:fzf_root = get(g:, 'fzf_root', g:plug_root . 'fzf')
if has('win32')
    let g:fzf_executable_file = 'fzf.exe'
else
    let g:fzf_executable_file = 'fzf'
endif
let g:fzf_executable = get(g:, 'fzf_executable', g:fzf_root . '/bin/' . g:fzf_executable_file)

" Default fzf action {{{
" An action can be a reference to a function that processes selected lines
function! s:build_quickfix_list(lines)
    call setqflist(map(copy(a:lines), '{ "filename": v:val, "lnum": 1 }'))
    copen
    cc
endfunction

let g:fzf_action = {
            \   'ctrl-q': function('s:build_quickfix_list'),
            \   'ctrl-t': 'tab split',
            \   'ctrl-x': 'split',
            \   'ctrl-v': 'vsplit' 
            \ }
" }}}

" Default fzf layout{{{
" - Popup window (center of the screen)
let s:fzf_layout_center_of_screen = { 'window': { 'width': 0.9, 'height': 0.6 } }
" - Popup window (center of the current window)
let s:fzf_layout_center_of_current_window = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true } }
" - down / up / left / right
let s:fzf_layout_down = { 'down': '40%' }
let s:fzf_layout_up = { 'up': '40%' }
let s:fzf_layout_left = { 'left': '40%' }
let s:fzf_layout_right = { 'right': '40%' }
" - Window using a Vim command
let s:fzf_layout_enew = { 'window': 'enew' }
let g:fzf_layout_tabnew = { 'window': '-tabnew' }
let g:fzf_layout_new_10 = { 'window': '10new' }
" let g:fzf_layout = get(g:, 'fzf_layout', s:fzf_layout_center_of_current_window)
let g:fzf_layout = s:fzf_layout_center_of_current_window
"}}}

" Default fzf colors{{{
" Customize fzf colors to match your color scheme
" - fzf#wrap translates this to a set of `--color` options
let g:fzf_colors = {
            \ 'fg':      ['fg', 'Normal'],
            \ 'bg':      ['bg', 'Normal'],
            \ 'hl':      ['fg', 'Comment'],
            \ 'fg+':     ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
            \ 'bg+':     ['bg', 'CursorLine', 'CursorColumn'],
            \ 'hl+':     ['fg', 'Statement'],
            \ 'info':    ['fg', 'PreProc'],
            \ 'border':  ['fg', 'Ignore'],
            \ 'prompt':  ['fg', 'Conditional'],
            \ 'pointer': ['fg', 'Exception'],
            \ 'marker':  ['fg', 'Keyword'],
            \ 'spinner': ['fg', 'Label'],
            \ 'header':  ['fg', 'Comment']
            \ }
"}}}

let g:fzf_history_dir = '~/.local/share/fzf-history'

let g:fzf_vim = get(g:, 'fzf_vim', { 'command_prefix': 'Fzf' })
let g:fzf_vim.command_prefix = 'Fzf'
" This is the default option:
"   - Preview window on the right with 50% width
"   - CTRL-/ will toggle preview window.
" - Note that this array is passed as arguments to fzf#vim#with_preview function.
" - To learn more about preview window options, see `--preview-window` section of `man fzf`.
let g:fzf_vim.preview_window = ['right,50%', 'ctrl-/']


let g:fzf_preview_bash = 'C:\Programs\Git\bin\bash.exe'
" [Buffers] Jump to the existing window if possible
let g:fzf_vim.buffers_jump = 1
" [[B]Commits] Customize the options used by 'git log':
let g:fzf_vim.commits_log_options = '--graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr"'
" [Tags] Command to generate tags file
let g:fzf_vim.tags_command = 'ctags -R'
" [Commands] --expect expression for directly executing the command
let g:fzf_vim.commands_expect = 'alt-enter,ctrl-x'
let $FZF_DEFAULT_OPTS='--color=bg+:#3F3F3F,bg:#4B4B4B,border:#6B6B6B,spinner:#98BC99,hl:#719872,fg:#D9D9D9,header:#719872,info:#BDBB72,pointer:#E12672,marker:#E17899,fg+:#D9D9D9,preview-bg:#3F3F3F,prompt:#98BEDE,hl+:#98BC99'

function! plugin#fzf#load() abort
    if !exists('g:loaded_fzf')
        packadd fzf
        runtime! fzf.vim
    endif
    if !exists('g:loaded_fzf_vim')
        packadd fzf.vim
        runtime! fzf.vim
    endif
    return exists('g:loaded_fzf') && exists('g:loaded_fzf_vim')
endfunction

function! plugin#fzf#init() abort
	if plugin#fzf#load()
		if executable(g:fzf_executable)
			let g:fzf_executable_path = fnamemodify(g:fzf_executable, ':p:h')
			" echo " g:fzf_executable_path = " . g:fzf_executable_path
			let $PATH = $PATH . ';' . g:fzf_executable_path
		else
			echom "Installing fzf"
			call fzf#install()
		endif
		nnoremap <m-f> :FzfFiles<cr>
		nmap <m-b> :FzfBuffers<cr>
		return v:true
	endif
	return v:false
endfunction

